package com.wuming.caiwu.service.impl;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.WumingErrorCode;
import com.wuming.caiwu.dao.SysRoleMapper;
import com.wuming.caiwu.model.SysRole;
import com.wuming.caiwu.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sysRoleService")
public class SysRoleServiceImpl extends BaseServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public void saveSysRole(SysRole member) throws BusinessException {
        member.setCreateTime(new Date());
        sysRoleMapper.insert(member);
    }

    @Override
    public void modifySysRole(SysRole member) throws BusinessException {
        sysRoleMapper.updateByPrimaryKey(member);
    }

    @Override
    public SysRole getSysRoleById(Long id) throws BusinessException {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void removeSysRoleById(Long id) throws BusinessException {
        sysRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SysRole> getSysRolesByPage(String name,Integer storeId, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(name)) {
            params.put("name", name);
        }
        if(null != storeId){
            params.put("storeId", storeId);
        }
        try {
            return (List<SysRole>) this.getPageList(SysRoleMapper.class, "selectByParams", params,pageIndex,pageSize);
        } catch (Exception e) {
            logger.error("查询会员信息总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(String name,Integer storeId) throws BusinessException {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(name)) {
            params.put("name", name);
        }
        if(null != storeId){
            params.put("storeId", storeId);
        }
        try {
            return this.getTotal(SysRoleMapper.class, "selectTotal", params);
        } catch (Exception e) {
            logger.error("查询会员信息总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public List<SysRole> getAllRoles() throws BusinessException {
        return sysRoleMapper.selectAll();
    }

    @Override
    public List<SysRole> getRoleByParams(Integer storeId,String name, Integer isMemberRole,Integer isSystemRole) throws BusinessException {
        Map<String, Object> params = new HashMap<String, Object>();
        if(null != storeId){
            params.put("storeId", storeId);
        }
        if (StringUtils.isNotBlank(name)) {
            params.put("name", name);
        }
        if(null != isMemberRole){
            params.put("isMemberRole", isMemberRole);
        }
        if(null != isSystemRole){
            params.put("isSystemRole", isSystemRole);
        }
        return sysRoleMapper.selectByConditions(params);
    }
}
